package com.koron.standard.jobStock.overhaul.controller;


import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.deviceClasssify.service.TypeService;
import com.koron.standard.jobStock.overhaul.bean.dto.OverhaulDTO;
import com.koron.standard.jobStock.overhaul.bean.dto.OverhaulMajorDTO;
import com.koron.standard.jobStock.overhaul.bean.query.OverhaulQuery;
import com.koron.standard.jobStock.overhaul.bean.vo.OverhaulVO;
import com.koron.standard.jobStock.overhaul.excel.OverhaulListener;
import com.koron.standard.jobStock.overhaul.service.OverhaulService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * 大修标准信息(Overhaul)表控制层
 *
 * @author jzp
 * @since 2021-09-16
 */
@Api(tags = "大修标准信息接口")
@RestController
@RequestMapping("overhaul")
public class OverhaulController {

    @Autowired
    private OverhaulService overhaulService;

    @Autowired
    private TypeService typeService;

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<Page<OverhaulVO>> page(PageQuery<OverhaulVO> pageQuery, OverhaulQuery query) {
        Page<OverhaulVO> page = overhaulService.page(pageQuery, query);
        return Response.succ(page, page.getTotal());
    }

    @ApiOperation(value = "查询所有")
    @GetMapping("list")
    public Response<List<OverhaulMajorDTO>> list(OverhaulQuery query) {
        List<OverhaulMajorDTO> majors = overhaulService.listDto(query);
        return Response.ok(majors);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<OverhaulDTO> select(@RequestParam String id) {
        OverhaulDTO overhaulDTO = overhaulService.getDtoById(id);
        return Response.ok(overhaulDTO);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Response save(@RequestBody OverhaulDTO overhaulDTO) {
        overhaulService.saveDto(overhaulDTO);
        return Response.ok(overhaulDTO.getId());
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public Response update(@RequestBody OverhaulDTO overhaulDTO) {
        overhaulService.updateDtoById(overhaulDTO);
        return Response.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping
    public Response delete(@RequestParam("ids") List<String> ids) {
        overhaulService.removeByIds(ids);
        return Response.ok();
    }

    @ApiOperation(value = "设置状态")
    @PutMapping("updateStatus")
    public Response updateStatus(@RequestParam String id, @RequestParam Integer status) {
        overhaulService.updateStatus(id, status);
        return Response.ok();
    }

    @ApiOperation(value = "设置为公共标准")
    @PutMapping("updateToPublic")
    public Response updateToPublic(@RequestParam("ids") List<String> ids) {
        overhaulService.updateToPublic(ids);
        return Response.ok();
    }

    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/job_overhaul_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("大修标准", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        book.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        OverhaulListener overhaulListener = new OverhaulListener(typeService, overhaulService);
        EasyExcel.read(file.getInputStream(), overhaulListener).doReadAll();
        return overhaulListener.getSheetResponse();
    }

}
